package br.ufpe.cin.rdfilter.gui;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Vector;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import br.ufpe.cin.rdfilter.model.Manager;
import br.ufpe.cin.rdfilter.model.Query;
import br.ufpe.cin.rdfilter.model.User;
import br.ufpe.cin.rdfilter.query.QueryManager;


@SuppressWarnings("serial")
public class FeedBackGui extends javax.swing.JFrame {


	public FeedBackGui(List<String> columnsName, String idQuery, String idUser, List<String> dataQuery) {
		this.columnsName = columnsName;
		this.idQuery = idQuery;
		this.dataQuery = dataQuery;
		this.idUser = idUser;

		initComponents();
		setVisible(true);
	}
	
	public FeedBackGui(QueryManager qm, Query q, User u){
		this.qm=qm;
		this.q=q;
		this.u=u;
		initComponents();
		setVisible(true);
	}


	private void initComponents() {

		buttonGroup1 = new javax.swing.ButtonGroup();
		jScrollPane1 = new javax.swing.JScrollPane();
		resultTable = new javax.swing.JTable();
		feedbackPanel = new javax.swing.JPanel();
		tpRadioButton = new javax.swing.JRadioButton();
		fpRadioButton = new javax.swing.JRadioButton();
		fnRadioButton = new javax.swing.JRadioButton();
		confTextField = new javax.swing.JFormattedTextField();
		confLabel = new javax.swing.JLabel();
		saveButton = new javax.swing.JButton();
		triplePanel = new javax.swing.JPanel();
		sLabel = new javax.swing.JLabel();
		oLabel = new javax.swing.JLabel();
		pLabel = new javax.swing.JLabel();
		sTempLabel = new javax.swing.JLabel();
		oTempLabel = new javax.swing.JLabel();
		pTempLabel = new javax.swing.JLabel();
		notesButton = new javax.swing.JButton();
		sendButton = new javax.swing.JButton();
		feedback = new Vector<String>();
		try {
			writer = new FileWriter(new File("notes.txt"), true);
		} catch (IOException e) {e.printStackTrace();}

		setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);

		tableCreator();
		resultTable.setToolTipText("");
		resultTable.setName("resultsTable");
		jScrollPane1.setViewportView(resultTable);
		resultTable.setCellSelectionEnabled(true);
		resultTable.addMouseListener(new MouseTable());

		feedbackPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Feedback"));

		buttonGroup1.add(tpRadioButton);
		tpRadioButton.setLabel("True Positive");
		tpRadioButton.setSelected(true);


		buttonGroup1.add(fpRadioButton);
		fpRadioButton.setLabel("False Positive");


		buttonGroup1.add(fnRadioButton);
		fnRadioButton.setLabel("False Negative");

		confTextField.setText("1.0");

		confLabel.setText("Confidence Level:");

		saveButton.setText("Save");
		saveButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				saveButtonActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout feedbackPanelLayout = new javax.swing.GroupLayout(feedbackPanel);
		feedbackPanel.setLayout(feedbackPanelLayout);
		feedbackPanelLayout.setHorizontalGroup(
				feedbackPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(feedbackPanelLayout.createSequentialGroup()
						.addGroup(feedbackPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addComponent(tpRadioButton)
								.addComponent(fpRadioButton)
								.addComponent(fnRadioButton))
								.addGap(20, 20, 20)
								.addGroup(feedbackPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
										.addGroup(feedbackPanelLayout.createSequentialGroup()
												.addComponent(confLabel)
												.addGap(18, 18, 18)
												.addComponent(confTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
												.addComponent(saveButton))
												.addContainerGap(14, Short.MAX_VALUE))
		);
		feedbackPanelLayout.setVerticalGroup(
				feedbackPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(feedbackPanelLayout.createSequentialGroup()
						.addGroup(feedbackPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addGroup(feedbackPanelLayout.createSequentialGroup()
										.addComponent(tpRadioButton)
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(fpRadioButton)
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(fnRadioButton)
										.addGap(0, 0, Short.MAX_VALUE))
										.addGroup(feedbackPanelLayout.createSequentialGroup()
												.addContainerGap()
												.addGroup(feedbackPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(confLabel)
														.addComponent(confTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
														.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
														.addComponent(saveButton)))
														.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);

		triplePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Selected"));

		sLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); 
		sLabel.setText(columnsName.get(0));

		oLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); 
		oLabel.setVisible(false);

		if(columnsName.size() > 1){
			oLabel.setVisible(true);
			oLabel.setText(columnsName.get(1));
		}

		pLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); 
		pLabel.setVisible(false);
		//pLabel.setText("Predicate:");

		if(columnsName.size() == 3){
			pLabel.setVisible(true);
			pLabel.setText(columnsName.get(2));
		}

		javax.swing.GroupLayout triplePanelLayout = new javax.swing.GroupLayout(triplePanel);
		triplePanel.setLayout(triplePanelLayout);
		triplePanelLayout.setHorizontalGroup(
				triplePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(triplePanelLayout.createSequentialGroup()
						.addContainerGap()
						.addGroup(triplePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addComponent(sTempLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addGroup(triplePanelLayout.createSequentialGroup()
										.addGroup(triplePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(sLabel)
												.addComponent(oLabel)
												.addComponent(pLabel))
												.addGap(0, 0, Short.MAX_VALUE))
												.addComponent(oTempLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
												.addComponent(pTempLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
												.addContainerGap())
		);
		triplePanelLayout.setVerticalGroup(
				triplePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(triplePanelLayout.createSequentialGroup()
						.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(sLabel)
						.addGap(1, 1, 1)
						.addComponent(sTempLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addGap(1, 1, 1)
						.addComponent(oLabel)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(oTempLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addGap(1, 1, 1)
						.addComponent(pLabel)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(pTempLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
		);

		notesButton.setText("Notes");
		notesButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				notesButtonActionPerformed(evt);
			}
		});

		sendButton.setText("Send");
		sendButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				sendButtonActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 873, Short.MAX_VALUE)
						.addGap(18, 18, 18)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
								.addComponent(triplePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(feedbackPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addGroup(layout.createSequentialGroup()
										.addComponent(notesButton, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(sendButton, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(16, 16, 16)))
										.addContainerGap())
		);
		layout.setVerticalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
				.addGroup(layout.createSequentialGroup()
						.addComponent(triplePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addGap(18, 18, 18)
						.addComponent(feedbackPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addComponent(sendButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(notesButton, javax.swing.GroupLayout.DEFAULT_SIZE, 35, Short.MAX_VALUE))
								.addGap(20, 20, 20))
		);

		pack();
	}



	protected void sendButtonActionPerformed(ActionEvent evt) {
		//File arq = new File("notes.txt");
		try {
			for(int i =0; i < feedback.size(); i++){
				writer.write(feedback.get(i));
			} 
			writer.flush();
		}
		catch (IOException e) {e.printStackTrace();}

	}


	private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {
		//Gambiarra =)


		String radioButtom;
		if(tpRadioButton.isSelected()){
			radioButtom = "True Positive";
		}else if(fpRadioButton.isSelected()){
			radioButtom = "False Positive";
		}else{
			radioButtom = "False Negative";
		}

		
		
		feedback.add(idQuery + ";" + idUser + ";" + sTempLabel.getText()+ ";"  + radioButtom + ";"+ confTextField.getText() + "\n");
		
		/**if(columnsName.size() == 1){
			feedback.add(idQuery + ";" + sTempLabel.getText()+ ";"  + radioButtom + ";"+ confTextField.getText() + "\n");
		}else if(columnsName.size() == 2){
			feedback.add(idQuery + ";" + sTempLabel.getText()+ ";" + oTempLabel.getText() + ";"  + radioButtom + ";" + confTextField.getText() + "\n");
		}else if(columnsName.size() == 3){
			feedback.add(idQuery + ";" + sTempLabel.getText()+ ";" + oTempLabel.getText() + ";" + pTempLabel.getText() +  ";" + radioButtom + ";" + confTextField.getText() + "\n");
		}*/
		
	}

	private void notesButtonActionPerformed(java.awt.event.ActionEvent evt) {
		
//		new Notes(columnsName, feedback);
		new Notes(feedback);
		
	}

	private void tableCreator(){
		//CRIANDO TABELA

		  
		String[] row = new String[columnsName.size()];
		String[] colunas = new String[columnsName.size()];

		for(int i = 0; i < columnsName.size(); i++){
			colunas[i] = columnsName.get(i);
		}

		
		int x = 0;

		DefaultTableModel modelo = new DefaultTableModel(null, colunas);
		resultTable = new JTable( modelo );

		while(x<dataQuery.size()){
			
			for(int i = 0; i < columnsName.size(); i++){

				row[i] = dataQuery.get(x);
				x++;
			}
			modelo.addRow(row);

		}

		//FIM DE CRIANDO TABELA
	}


	private javax.swing.ButtonGroup buttonGroup1;
	private javax.swing.JLabel confLabel;
	private javax.swing.JFormattedTextField confTextField;
	private javax.swing.JPanel feedbackPanel;
	private javax.swing.JRadioButton fnRadioButton;
	private javax.swing.JRadioButton fpRadioButton;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JButton notesButton;
	private javax.swing.JLabel oLabel;
	private javax.swing.JLabel oTempLabel;
	private javax.swing.JLabel pLabel;
	private javax.swing.JLabel pTempLabel;
	private javax.swing.JTable resultTable;
	private javax.swing.JLabel sLabel;
	private javax.swing.JLabel sTempLabel;
	private javax.swing.JButton saveButton;
	private javax.swing.JButton sendButton;
	private javax.swing.JRadioButton tpRadioButton;
	private javax.swing.JPanel triplePanel;
	private FileWriter writer;
	private List<String> columnsName;
	private String idQuery;
	private List<String> feedback;
	private List<String> dataQuery;
	private String idUser;
	
	private QueryManager qm;
	private Query q;
	private User u;

	private class MouseTable extends MouseAdapter {  

		@Override  
		public void mouseClicked(MouseEvent me) {  
			Point click = new Point(me.getX(), me.getY());  

			int row = resultTable.rowAtPoint(click);  
			int col = resultTable.columnAtPoint(click);

			sTempLabel.setText((String) resultTable.getValueAt(row,col));
			sTempLabel.setFont(new java.awt.Font("Tahoma", 0, 11));

			tpRadioButton.setSelected(true);
			fpRadioButton.setSelected(false);
			fnRadioButton.setSelected(false);

			confTextField.setText("1.0");


		}  
	}

}
